package com.itheima.health.service.impl;

import com.itheima.health.common.RedisConst;
import com.itheima.health.dao.MemberDao;
import com.itheima.health.dao.OrderDao;
import com.itheima.health.dao.OrderSettingDao;
import com.itheima.health.dto.OrderMessageDTO;
import com.itheima.health.dto.OrderSettingDTO;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.Member;
import com.itheima.health.pojo.Order;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderService;
import com.itheima.health.util.SnowflakeIdUtil;
import com.itheima.health.vo.OrderSubmitParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.Date;

/**
 * 预约ServiceImpl
 */
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderSettingDao orderSettingDao;
    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 新增预约信息
     *
     * @param orderSubmitParam 预约信息
     */
    @Override
    @Transactional
    public Order saveOrder(OrderSubmitParam orderSubmitParam) {
        //获取用户手机号
        String telephone = orderSubmitParam.getTelephone();
        //先获取验证码
        String codeInRedis = (String) redisTemplate.boundValueOps(RedisConst.VALIDATE_CODE_PREFIX + telephone).get();
        //判断验证码是否正确
        if (!orderSubmitParam.getValidateCode().equals(codeInRedis)) {
            throw new BusinessRuntimeException("验证码错误，请重新输入！");
        }
        //创建member对象
        Member member;
        //验证码正确的话，就判断给用户是否已经注册为会员
        member = memberDao.selectMemberByPhone(telephone);
        if (member != null) {
            //用户已注册,更新会员信息
            //设置姓名
            member.setName(orderSubmitParam.getName());
            //设置性别
            member.setSex(orderSubmitParam.getSex());
            //设置身份证
            member.setIdCard(orderSubmitParam.getIdCard());
            //更新
            memberDao.updateMember(member);
            //判断该用户是否已经预约过
            List<Order> orderList = orderDao.selectOrderByMemberId(member.getId());
            //用户已经预约过了
            if (orderList.size() > 0) {
                //返回true，说明已经预约
                throw new BusinessRuntimeException("该用户已预约，不要重复预约！");
            }
        } else {
            //用户未注册，先注册为会员
            member = new Member();
            //设置姓名
            member.setName(orderSubmitParam.getName());
            //设置性别
            member.setSex(orderSubmitParam.getSex());
            //设置身份证
            member.setIdCard(orderSubmitParam.getIdCard());
            //设置手机号
            member.setPhoneNumber(telephone);
            //获取当前时间，为注册时间
            member.setRegTime(new Date());
            //新增用户
            memberDao.insertMember(member);
        }
        //没有预约过，生成新的预约订单
        Order order = new Order();
        //设置会员id
        order.setMemberId(member.getId());
        //设置预约日期
        order.setOrderDate(orderSubmitParam.getOrderDate());
        //设置预约类型，默认为微信预约
        order.setOrderType("微信预约");
        //设置预约状态，默认为未到诊
        order.setOrderStatus("未到诊");
        //设置体检套餐id
        order.setSetmealId(orderSubmitParam.getSetMealId());
        //新增预约订单
        orderDao.insertOrder(order);
        //修改对应日期的已预约人数,+1
        orderSettingDao.updateReservations(order.getOrderDate());
        //返回订单数据
        return order;
    }

    /**
     * 根据id查询预约订单详情
     *
     * @param id 订单id
     */
    @Override
    public OrderMessageDTO getOrderMessage(Integer id) {
        return orderDao.selectOneOrderMessage(id);
    }

}
